import { Injectable } from '@angular/core';
import { SubjectService } from '@core/services/events/subject.service';
import { ApesMessageService } from '@shared/apes/message';
import { ExpressionService } from '@core/services/expression/ExpressionService';
import { deepClone, getValueUsingField, isObject } from '../../../utils/utils';
import { Events } from '@core/services/events/Events';
import { FormService } from '@core/services/form.service';
import { ApiService } from '@core/services/http/api.service';

export interface LinkType {
  id: string;
  ctx: any;
  api: string;
  config: any;
  name?: string;
  condition?: any;
  callback?: string;
  data?: any;
}


@Injectable({
  providedIn: 'root',
})
export class LinkService {

  ctx: any;
  data: any;

  constructor(
    private msg: ApesMessageService,
    private subject: SubjectService,
    private event: Events,
    private expressionService: ExpressionService,
    private formService: FormService,
    private apiService: ApiService,
  ) {
  }


  startLinkData(param: LinkType) {
    console.log(param);
    this.data = param.data ? param.data : {};
    this.ctx = param.ctx;
    let config = deepClone(param.config);
    let callbackBody = param.callback;


    let api = this.checkApi(param.api) ? this.checkApi(param.api) : param.api;

    config.api = this.checkApi(config.api) ? this.checkApi(config.api) : config.api;

    let resultData = {};
    if (callbackBody) {
      let node = { ...param };
      resultData = this.expressionService.evaluate(callbackBody, {
        context: this.ctx,
        model: this.ctx['model'],
        data: this.data,
        node: node,
      }, true);
    } else {
      resultData = this.data;
    }
    if (this.data.hasOwnProperty('FORM_ID')) {
      api = this.data['FORM_ID'];
      config.api = this.data['API'];
    }

    if (api.indexOf('app/') != -1) {
      let href = window.location.href.split('#');
      let h = href[0].split('erp');
      this.event.publish('drawer');
      window.open(h[0] + api, '_blank');
      return;
    }

    this.openContextLink(resultData, api, config);
  }


  private checkApi(api) {
    return this.expressionService.evaluate(api, {
      context: this.ctx,
      model: this.ctx['model'],
      data: this.data,
    });
  }

  private openContextLink(data: any, method: string, params?: object) {
    if (isObject(params)) Object.assign(data, params);
    if (method == null) {
      this.msg.error('该链接尚未存在，麻烦核实！', { 'apesDuration': 5000 });
      return false;
    }
    if(params['OAInterface'] == false){
      const formId = method;
      data['formId'] = formId;
      this.formService.navigateToForm(data);
      return;
    }
    this.apiService.post(method, { 'body': JSON.stringify(data) }).subscribe((item) => {
      if( data.changeSignFlag ) item.changeSignFlag = data.changeSignFlag;
      if( data.wfFlag ) item.wfFlag = data.wfFlag;
      if( data.OAInterface ) item.OAInterface = data.OAInterface;

      this.formService.navigateToForm(item);
    });
  }
}
